//
//  ViewController.swift
//  Calculator
//
//  Created by mac2 on 2017/3/30.
//  Copyright © 2017年 mac2. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    var userIsInTheMiddleOfTying = false

    @IBOutlet weak var display: UILabel!
    
    @IBAction func touchDigit(_ sender: UIButton) {
        let digit = sender.currentTitle!
        let currentDisplayText = display.text!
        if userIsInTheMiddleOfTying {
            display!.text = currentDisplayText + digit
        }
        else {
            display.text = digit
            userIsInTheMiddleOfTying = true
        }
    }
    
    var displayValue: Double {
        get {
            return Double(display.text!)!
        }
        set {
            display.text = String(newValue)
        }
    }
    
    private var brain = CalculatorBrain()
    
    @IBAction func specialMark(_ sender: UIButton) {
        if userIsInTheMiddleOfTying {
            brain.setOperation(displayValue)
            userIsInTheMiddleOfTying = false
        }
        if let mathmeticalSymbol = sender.currentTitle {
            brain.perfromOperatin(mathmeticalSymbol)
        }
        if let result = brain.result {
            displayValue = result
        }
    }
    
}













